package com.shishuo.cms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shishuo.cms.dao.PictureDao;
import com.shishuo.cms.dao.ProductDao;
import com.shishuo.cms.entity.Picture;
import com.shishuo.cms.entity.Product;
import com.shishuo.cms.entity.vo.PageVo;

@Service
public class ProductService {

	@Autowired
	private ProductDao productDao;
	@Autowired
	private PictureDao pictureDao;

	public Product addProduct(Product product) {
		productDao.addProduct(product);
		if (product.getPictureList()!=null) {
			for (Picture picture : product.getPictureList()) {
				picture.setProductId(product.getProductId());
				pictureDao.addPicture(picture);
			}
		}
		return product;
	}

	public int updateProduct(Product product) {
		int res=productDao.updateProduct(product);
		if (product.getPictureList()!=null) {
			for (Picture picture : product.getPictureList()) {
				picture.setProductId(product.getProductId());
				pictureDao.addPicture(picture);
			}
		}
		return res;
	}

	public int deleteProduct(long productId) {
		return productDao.deleteProduct(productId);
	}

	public Product getProductById(long productId) {
		Product product=productDao.getProductByProductId(productId);
		product.setPictureList(pictureDao.getPictureByProductId(productId));
		return product;
	}
	

	public PageVo<Product> getProductPage(int pageNum,long folderId) {
		PageVo<Product> pageVo = new PageVo<Product>(pageNum);
		pageVo.setRows(12);
		List<Product> list = productDao.selectByCondition(folderId,
				pageVo.getOffset(), pageVo.getRows());
		pageVo.setList(list);
		pageVo.setCount(productDao.selectCountByPage(folderId));
		return pageVo;
	}

}
